/*     */ package cc.s2m.web.s2mBlog.task;
/*     */ 
/*     */ import cc.s2m.web.s2mBlog.pojo.SysTaskHandel;
/*     */ import cc.s2m.web.s2mBlog.pojo.SysTaskLog;
/*     */ import cc.s2m.web.s2mBlog.service.ISysTaskHandel;
/*     */ import cc.s2m.web.s2mBlog.service.ISysTaskLog;
/*     */ import java.net.Inet4Address;
/*     */ import java.net.InetAddress;
/*     */ import java.net.NetworkInterface;
/*     */ import java.net.SocketException;
/*     */ import java.net.UnknownHostException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Enumeration;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.annotation.PostConstruct;
/*     */ import org.apache.logging.log4j.LogManager;
/*     */ import org.apache.logging.log4j.Logger;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ 
/*     */ public abstract class BaseTask
/*     */ {
/*  27 */   private static Logger log = LogManager.getLogger(BaseTask.class);
/*     */ 
/*     */   @Autowired
/*     */   private ISysTaskHandel sysTaskHandel;
/*     */ 
/*     */   @Autowired
/*     */   private ISysTaskLog sysTaskLog;
/*     */ 
/*  36 */   public void run() { Map map = new HashMap();
/*  37 */     map.put("moduleName", getModule());
/*  38 */     map.put("hostname", getLocalHostName());
/*  39 */     map.put("isEnabled", Boolean.valueOf(true));
/*  40 */     List list = this.sysTaskHandel.getList(new SysTaskHandel(), map);
/*  41 */     if ((list == null) || (list.size() < 1)) {
/*  42 */       return;
/*     */     }
/*  44 */     SysTaskLog bean = new SysTaskLog();
/*  45 */     bean.setModuleName(getModule());
/*  46 */     bean.setHostname(getLocalHostName());
/*  47 */     bean.setIssuccess(Boolean.valueOf(false));
/*  48 */     bean.setIpAddress(getLocalHostIps());
/*  49 */     this.sysTaskLog.insert(bean);
/*  50 */     bean = (SysTaskLog)this.sysTaskLog.selectByPrimaryKey(bean.getId());
/*     */     try {
/*  52 */       doTask();
/*     */     } catch (Exception e) {
/*  54 */       log.error("自动任务执行出错：", e);
/*  55 */       this.sysTaskLog.updateByPrimaryKey(bean);
/*     */     }
/*  57 */     bean.setIssuccess(Boolean.valueOf(true));
/*  58 */     this.sysTaskLog.updateByPrimaryKey(bean);
/*     */   }
/*     */ 
/*     */   @PostConstruct
/*     */   public void rsgisterTask()
/*     */     throws Exception
/*     */   {
/*  70 */     Map map = new HashMap();
/*  71 */     map.put("moduleName", getModule());
/*  72 */     map.put("hostname", getLocalHostName());
/*  73 */     List list = this.sysTaskHandel.getList(new SysTaskHandel(), map);
/*  74 */     if ((list == null) || (list.size() < 1))
/*     */     {
/*  76 */       SysTaskHandel bean = new SysTaskHandel();
/*  77 */       bean.setHostname(getLocalHostName());
/*  78 */       bean.setModuleName(getModule());
/*  79 */       bean.setIpAddress(getLocalHostIps());
/*  80 */       bean.setIsEnabled(Boolean.valueOf(false));
/*     */ 
/*  82 */       this.sysTaskHandel.insert(bean);
/*     */     }
/*     */   }
/*     */ 
/*     */   public String getModule() {
/*  87 */     return getClass().getSimpleName();
/*     */   }
/*     */ 
/*     */   public String getLocalHostName() {
/*     */     try {
/*  92 */       return InetAddress.getLocalHost().getHostName();
/*     */     } catch (UnknownHostException e) {
/*  94 */       throw new RuntimeException("Unknown Host", e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public String getLocalHostIps() {
/*  99 */     List<String> ips = new ArrayList();
/* 100 */     Enumeration allNetInterfaces = null;
/*     */     try {
/* 102 */       allNetInterfaces = NetworkInterface.getNetworkInterfaces();
/*     */     } catch (SocketException e) {
/* 104 */       throw new RuntimeException(e.getMessage(), e);
/*     */     }
/* 106 */     while (allNetInterfaces.hasMoreElements()) {
/* 107 */       NetworkInterface netInterface = (NetworkInterface)allNetInterfaces.nextElement();
/* 108 */       Enumeration addresses = netInterface.getInetAddresses();
/* 109 */       while (addresses.hasMoreElements()) {
/* 110 */         InetAddress inetAddress = (InetAddress)addresses.nextElement();
/* 111 */         if ((inetAddress != null) && ((inetAddress instanceof Inet4Address))) {
/* 112 */           String ip = inetAddress.getHostAddress();
/* 113 */           if ((ip != null) && (!ip.equals("127.0.0.1"))) {
/* 114 */             ips.add(ip);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 119 */     if (ips.isEmpty()) {
/* 120 */       return null;
/*     */     }
/* 122 */     StringBuffer strBuffer = new StringBuffer();
/* 123 */     for (String str : ips) {
/* 124 */       strBuffer.append("," + str);
/*     */     }
/* 126 */     return strBuffer.toString().substring(1);
/*     */   }
/*     */ 
/*     */   protected abstract void doTask();
/*     */ }

/* Location:           C:\Users\Administrator\Desktop\s2mBlog.zip
 * Qualified Name:     s2mBlog-3.0.0.WEB-INF.classes.cc.s2m.web.s2mBlog.task.BaseTask
 * JD-Core Version:    0.6.1
 */